package jt.classic.system.objectquery;

import org.limp.basework.console.XSystemConfig;

import java.io.File;
import java.io.FileFilter;

import java.util.Timer;
import java.util.TimerTask;


/**
 * 对象查询初始化定时器
 *
 * @author mido
 *
 */
public class QueryOTimer {
    private Timer timer = new Timer();
    private long delay = 5000;
    private long period = 15000;

    /**
     * 构造函数
     * @param delay
     * @param period
     */
    public QueryOTimer(long delay, long period) {
        if (delay > 0) {
            this.delay = delay;
        }

        if (period > 0) {
            this.period = period;
        }
    }

    /**
     * 运行任务
     */
    public void runTask() {
        System.out.println("system message: object-query task is running...");
        timer.schedule(new MyTask(), this.delay, this.period);
    }

    /**
     * 停止任务
     */
    public void stopTask() {
        System.out.println("system message: object-query is cancelled...");
        timer.cancel();
    }

    /**
     *
     * @author mido
     *
     */
    private class MyTask extends TimerTask {
        public void run() {
            String systemHome = (String) XSystemConfig.config().getProperty(XSystemConfig.XSYSTEM_SYSTEM_HOME);

            File folder = new File(systemHome, "conf" + File.separator +
                    "queryO");

            if (folder.isDirectory()) {
                File[] files = folder.listFiles(new QueryFileFilter());

                if ((files != null) && (files.length > 0)) {
                    QueryOFiles tqf = QueryOFiles.getInstance();

                    for (int i = 0; i < files.length; i++) {
                        String fileName = files[i].getName();
                        long thisLastModified = files[i].lastModified();

                        long oldLastModified = tqf.getQueryFileLastModified(fileName);

                        if (thisLastModified != oldLastModified) {
                        	ObjectQueryRegister oqr = ObjectQueryRegister.getInstance();
                        	oqr.recallQueryFile(files[i]);
                        }
                    }
                }
            }
        }
    }

    /**
     * 表数据查询注册文件过滤器
     * @author mido
     *
     */
    private class QueryFileFilter implements FileFilter {
        /**
        *
        * 文件类型过滤
        * @param file 注册文件
        * @return boolean
        *
        */
        public boolean accept(File file) {
            if (file.isFile()) {
                /*文件名称*/
                String fileName = file.getName();

                /*文件扩展名*/
                String expandName = ((fileName.lastIndexOf(".") > -1) &&
                    (fileName.lastIndexOf(".") < fileName.length()))
                    ? fileName.substring(fileName.lastIndexOf(".") + 1) : "";

                return expandName.equals("queryO");
            }

            return false;
        }
    }
}
